﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Model;
using GPI.Server;
using Moon.Common.WebInfo;
using Moon.Common;
using System.IO;

public partial class Admin_ContentManage_ThemeSingle : System.Web.UI.Page
{
    NodeService server = new NodeService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {

                //获得查询的ID号
                string id = Request.QueryString["id"].ToString();
                this.btnSaveTemplate.CommandArgument = id;
                NodeInfo model = server.GetModel(id.ToInt(0));
                this.labTitle.Text = model.NodeName;
                if (model.Url.IsNullOrEmpty())
                {
                    model.Url = WebConfigInfo.ShowAction + model.Id;
                    new NodeService().Update(model);
                }
                //绑定内容编辑页面
                this.txtContent.Text = model.Content;
                this.txtImage.Text = model.NodeImage;
                this.btnSaveTemplate.CommandArgument = model.Id.ToString();

            }
            else
            {
                //this.form1.Visible = false;
                this.form1.InnerText = "404 Not Found!";
            }
        }
    }

    //保存模板
    protected void btnSaveTemplate_Click(object sender, EventArgs e)
    {
        //获得编辑内容
        NodeInfo model = server.GetModel(this.btnSaveTemplate.CommandArgument.ToInt(0));
        if (!model.IsHTML)
        {
            model.Url = WebConfigInfo.ShowAction + model.Id;
            model.Content = this.txtContent.Text;
            if (this.fuImage.HasFile&&model.NodeImage.IsNullOrEmpty())
                model.NodeImage = WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImage.FileName.GetFileExt();
            else
                model.NodeImage = this.txtImage.Text;
            if (uPFile(this.fuImage, model.NodeImage.GetFileName(), Application["WebPath"] + WebConfigInfo.ImgPath, WebConfigInfo.Imgformat) && server.Update(model))
                MessageBox.Show(this.Page, "操作成功！");
            else
                MessageBox.Show(this.Page, "操作失敗！");
        }
        else
        { }

    }


    /// <summary>
    /// 上傳操作
    /// </summary>
    /// <param name="Fupload">上傳空間</param>
    /// <param name="SaveName">保存名</param>
    /// <param name="dir">保存目錄</param>
    /// <param name="CheckType">驗證類型字符串</param>
    /// <returns></returns>
    private bool uPFile(System.Web.UI.WebControls.FileUpload Fupload, string SaveName, string dir, string CheckType)
    {
        if (!Fupload.HasFile)
        {
            return true;
        }

        /*如果上傳控件存在文件*/
        if (!this.txtImage.Text.IsNullOrEmpty())
        {
            CoFileHepler.DeleteFile(Application["WebPath"] + this.txtImage.Text);
        }

        /*
         * 判斷目錄是否存在不存在則進行創建
         */
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        try
        {
            //獲得文件類型
            string type = CoFileHepler.GetPostfixStr(Fupload.FileName);
            //判斷文件類型是否為需要的類型
            if (CoFileHepler.CheckValidExt(type, CheckType))
            {
                Fupload.PostedFile.SaveAs(dir + SaveName);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
}
